package pub

import (
	"context"
	"fmt"
	"github.com/apache/rocketmq-client-go/v2"
	"github.com/apache/rocketmq-client-go/v2/primitive"
	"github.com/pkg/errors"
	"log"
)

func SendAsyncMessage(p rocketmq.Producer, topic, message string) error {
	msg := primitive.NewMessage(topic,[]byte(message))
	err := p.SendAsync(context.Background(),
		func(ctx context.Context, result *primitive.SendResult, err error) {
			if err != nil {
				log.Printf("%+v",errors.Wrap(err,"ERR: fail to receive async message"))
				return
			}
			fmt.Printf("send message success: result=%s\n", result.String())
		},msg)
	if err != nil {
		return errors.Wrap(err,"ERR: fail to send async message")
	}
	return nil
}
